package 第七周;

/**
 * @author xhw
 */

public class 旋转图像 {
    public static void main(String[] args) {

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++)
                System.out.print(new int[][]{{1,2,3},{4,5,6},{7,8,9}}[i][j] + " ");
            //调用方法rotate
            rotate(new int[][]{{1,2,3},{4,5,6},{7,8,9}});

        }
    }
    public static void rotate(int[][] matrix) {
        int n = matrix.length;
        // 首先进行水平翻转
        for (int i = 0; i < n/2; i++) {
            for (int j = 0; j < n; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - 1 - i][j];
                matrix[n - 1 - i][j] = temp;
            }
        }
        // 再进行主对角线翻转
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }
}
